function changedOccured() {
	var cancel = document.getElementById('char_cancel');
	var save = document.getElementById('char_save');
	cancel.disabled = false;
	save.disabled = false;
}

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support AJAX");
	}
}

//Our XmlHttpRequest object to submit form
var saveReq = getXmlHttpRequestObject();

function handleSaveAck(){
	if (saveReq.readyState == 4) {
		if (saveReq.responseText == 'ok'){
			//update original data
			var char_busy_with = document.getElementById('char_busy_with');
			var original = document.getElementById('char_busy_with_original');
			original.innerHTML = char_busy_with.value;
			var char_location_time = document.getElementById('char_location_time');
			original = document.getElementById('char_location_time_original')
			original.innerHTML = char_location_time.value;
			var char_general_carac = document.getElementById('char_general_carac');
			original = document.getElementById('char_general_carac_original')
			original.innerHTML = char_general_carac.value;
			var char_carac_reaction = document.getElementById('char_carac_reaction');
			original = document.getElementById('char_carac_reaction_original')
			original.innerHTML = char_carac_reaction.value;
			var char_advantage_disadvantage = document.getElementById('char_advantage_disadvantage');
			original = document.getElementById('char_advantage_disadvantage_original')
			original.innerHTML = char_advantage_disadvantage.value;
			var char_skills = document.getElementById('char_skills');
			original = document.getElementById('char_skills_original')
			original.innerHTML = char_skills.value;
			var char_defense = document.getElementById('char_defense');
			original = document.getElementById('char_defense_original')
			original.innerHTML = char_defense.value;
			var char_movement = document.getElementById('char_movement');
			original = document.getElementById('char_movement_original')
			original.innerHTML = char_movement.value;
			var char_dark_corner = document.getElementById('char_dark_corner');
			original = document.getElementById('char_dark_corner_original')
			original.innerHTML = char_dark_corner.value;
		}else{
			//error
			var errorMsg = document.getElementById('char_errorMessage');
			errorMsg.className = 'char_errorMessage_show';
			var cancel = document.getElementById('char_cancel');
			var save = document.getElementById('char_save');
			cancel.disabled = false;
			save.disabled = false;
		}
	}
}

function save(){
	var errorMsg = document.getElementById('char_errorMessage');
	errorMsg.className = 'char_errorMessage';
	
	var char_busy_with = document.getElementById('char_busy_with');
	var char_location_time = document.getElementById('char_location_time');
	var char_general_carac = document.getElementById('char_general_carac');
	var char_carac_reaction = document.getElementById('char_carac_reaction');
	var char_advantage_disadvantage = document.getElementById('char_advantage_disadvantage');
	var char_skills = document.getElementById('char_skills');
	var char_defense = document.getElementById('char_defense');
	var char_movement = document.getElementById('char_movement');
	var char_dark_corner = document.getElementById('char_dark_corner');
	
	var cancel = document.getElementById('char_cancel');
	var save = document.getElementById('char_save');
	cancel.disabled = true;
	save.disabled = true;
	//submit form, using POST
	saveReq.open("POST", '/character/', true);
	saveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	saveReq.onreadystatechange = handleSaveAck;
	params = "busy_with="+encodeURIComponent(char_busy_with.value)
			+"&location_time="+encodeURIComponent(char_location_time.value)
			+"&general_carac="+encodeURIComponent(char_general_carac.value)
			+"&carac_reaction="+encodeURIComponent(char_carac_reaction.value)
			+"&advantage_disadvantage="+encodeURIComponent(char_advantage_disadvantage.value)
			+"&skills="+encodeURIComponent(char_skills.value)
			+"&defense="+encodeURIComponent(char_defense.value)
			+"&movement="+encodeURIComponent(char_movement.value)
			+"&dark_corner="+encodeURIComponent(char_dark_corner.value);
	saveReq.send(params);
}

function cancel(){
	var cancel = document.getElementById('char_cancel');
	var save = document.getElementById('char_save');
	cancel.disabled = true;
	save.disabled = true;
	//reload original data
	var char_busy_with = document.getElementById('char_busy_with');
	var original = document.getElementById('char_busy_with_original');
	char_busy_with.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_location_time = document.getElementById('char_location_time');
	original = document.getElementById('char_location_time_original');
	char_location_time.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_general_carac = document.getElementById('char_general_carac');
	original = document.getElementById('char_general_carac_original');
	char_general_carac.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_carac_reaction = document.getElementById('char_carac_reaction');
	original = document.getElementById('char_carac_reaction_original');
	char_carac_reaction.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_advantage_disadvantage = document.getElementById('char_advantage_disadvantage');
	original = document.getElementById('char_advantage_disadvantage_original');
	char_advantage_disadvantage.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_skills = document.getElementById('char_skills');
	original = document.getElementById('char_skills_original');
	char_skills.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_defense = document.getElementById('char_defense');
	original = document.getElementById('char_defense_original');
	char_defense.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_movement = document.getElementById('char_movement');
	original = document.getElementById('char_movement_original');
	char_movement.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
	var char_dark_corner = document.getElementById('char_dark_corner');
	original = document.getElementById('char_dark_corner_original');
	char_dark_corner.value = unescape(original.innerHTML).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
}
